/*
 This file 'Button' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2014 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2014-06-19 15:48
 */

define(function (require, exports, module) {

	require("Firebird.ui.Component");

	/**
	 * @class Firebird.ui.Button
	 *
	 * @example
	 * html
	 * <a id="J-save"
	 *    fb-ui="button"
	 *    fb-text="true"
	 *    fb-disabled="true"
	 *    fb-label="新增"
	 *    fb-icons="{primary: 'ui-icon-gear', secondary: 'ui-icon-triangle-1-s'}"
	 *    href="javascript:;"></a>
	 * js
	 * this.get("#J-save").setDisabled(true);
	 * this.get("#J-save").setText(true);
	 * this.get("#J-save").setLabel("保存");
	 */
	Firebird.define("Firebird.ui.Button", {
		extend: "Firebird.ui.Component",
		statics: {
			_uuid: 1,
			uuid: function () {
				return this._uuid++;
			},
			attrs: function ($component) {
				var attrs = {};
				attrs = $.extend({}, {
					renderTo: $component.get(0),
					instanceId: $component.attr("fb-instance") || $component.attr("name") || $component.attr("id") || ("button_" + Firebird.ui.Button.uuid()),

					disabled: Firebird.ui.Component.eval($component.attr("fb-disabled")),
					icons: Firebird.ui.Component.eval($component.attr("fb-icons")),
					label: $component.attr("fb-label") || "",
					text: Firebird.ui.Component.eval($component.attr("fb-text") || "true"),
					hidden: Firebird.ui.Component.eval($component.attr("fb-hidden"))
				});
				return attrs;
			}
		},
		config: {
			renderTo: null,
			instanceId: null,
			disabled: false,
			icons: {},
			label: "",
			text: true,
			hidden: false
		},

		instance: null,

		_init: function () {
			var me = this;
			var renderTo = me.renderTo = $(me.getRenderTo());
			me.instance = renderTo.data({
				instance: me,
				xtype: "button"
			});

			me.instance.button({});

			me.setLabel(me.getLabel());
			me.setText(me.getText());
			me.setDisabled(me.getDisabled());
			me.setIcons(me.getIcons() || {});
			me.setHidden(me.getHidden());
		},

		trigger: function (e) {
			this.instance.trigger.apply(this.instance, arguments);
		},

		on: function () {
			this.instance.on.apply(this.instance, arguments);
		},

		applyDisabled: function (status) {
			this.instance && this.instance.button("option", "disabled", status);
			return status;
		},

		applyIcons: function (icons) {
			this.instance && this.instance.button("option", "icons", icons);
			return icons;
		},

		applyLabel: function (label) {
			this.instance && this.instance.button("option", "label", label);
			return label;
		},

		applyText: function (text) {
			this.instance && this.instance.button("option", "text", text);
			return text;
		},

		applyHidden: function (status) {
			if (this.instance) {
				var status = !!status;
				this.instance.css({display: status ? "none" : ""});
			}
			return status;
		},

		destroy: function () {
			this.instance.button("destroy");
		},
		option: function (key, value) {
			if (typeof key == "undefined" && typeof value == "undefined") {
				return this.instance.button("option");
			}
			if (typeof value == "undefined") {
				return this.instance.button("option", key);
			}
			return this;
		},
		refresh: function () {

		},
		widget: function () {

		},
		getValue: function () {
			return this.getLabel();
		},
		setValue: function (value) {
			this.setLabel(value);
		}
	});

	module.exports = Firebird.ui.Button;

});